#include <bits/stdc++.h>

using namespace std;

// 不需要提交这个类
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// 修剪搜索二叉树
// 测试链接 : https://leetcode.cn/problems/trim-a-binary-search-tree/

class Solution 
{
public:
    // [low, high]
    TreeNode* trimBST(TreeNode* root, int low, int high) 
    {
        if(root == nullptr) return nullptr;


        if(root->val < low) return trimBST(root->right, low, high);
        if(root->val > high) return trimBST(root->left, low, high);
        // low ... root->val ... high
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
        return root;
    }
};